<?php

require_once './application/core/CoreBase.php';
require_once './library/Pclzip/pclzip.lib.php';
require_once './application/models/Tables/Droits.php';

class CommonCore extends CoreBase
{

    public function __construct(&$controller)
    {
        parent::__construct($controller);
    }

    public function GetMenu()
    {
        $session = new zend_session_namespace('session');
        $f = './application/configuration/menu/menu_' . $session->lang . '.ini';
        $r = fopen($f, 'r');
        $content = fread($r, filesize($f));
        fclose($r);
        $json = Zend_Json::decode($content);

        $i = 0;
        $html = "<ul id='nav'>";

        
        $tableDroit = new TableDroits();
        if (isset($session->idDroit) && $session->idDroit != 0)
        {
            $droit = $tableDroit->getDroit($session->idDroit);
        } else
        {
            $droit = $tableDroit->getDroit(0);
        }
        $lstDroit = explode(";", $droit[0]["Compo"]);

        while (isset($json['menu'][$i]))
        {
            $html2 = "<li>";
            $html2 .= "<a>";
            if (isset($json['menu'][$i]["item"]["image"]) && $json['menu'][$i]["item"]["image"] != "")
            {
                $html2 .= "<img src='" . $json['menu'][$i]["item"]["image"] . "' height='10'/>";
            }
            $html2 .= "" . $json['menu'][$i]["item"]["titre"] . "";
            $html2 .= "</a>";
            $j = 0;
            $k = 0;
            $html2 .= "<ul>";
            $sMenu = $json['menu'][$i]["item"]["sItem"];
            while (isset($sMenu[$j]))
            {
                if (in_array($sMenu[$j]["right"], $lstDroit))
                {
                    $html2 .= "<li><div class='content'><a href='javascript:" . $sMenu[$j]["onclick"] . "'>" . $sMenu[$j]["value"] . "</a></div></li>";
                    $k++;
                }
                $j++;
            }
            $html2 .= "</ul>";
            $html2 .= "</li>";
            if ($k > 0)
            {
                $html .= $html2;
            }
            $i++;
        }


        $html .= "</ul>";
        return $html;
    }

    public function GetTabControl()
    {
        /*
          $tabcontrol = new Zend_Session_Namespace('tabcontrol');
          if (isset($tabcontrol)) {

          }

          $header = "";
         */
        $html = '<div id="tabcontrol">';
        $html .= '<ul>';
        $html .= '<li class="titre_onglet"><h2>ONGLET HEADER</h2></li>';
        $html .= '<li class="contenu_onglet"><div></div></li>';
        $html .= '</ul>';
        $html .= '';
        $html .= '';
        $html .= '';
        $html .= '';
        $html .= '';
        $html .= '';
        $html .= '';
        $html .= '';
        $html .= '</div>';
        return $html;
    }

    public function unzip($file, $path='./public/tmp/', $effacer_zip=false)
    {
        $zip = new PclZip($file);
        if ($zip->extract(PCLZIP_OPT_PATH, $path,
                        PCLZIP_OPT_REMOVE_PATH, 'install/release') == 0)
        {
            return false;
        }
        return $zip->listContent();
    }

    public function deleteDir($path)
    {
        $open = @opendir($path);
        if (!$open)
        {
            return;
        }
        while ($file = readdir($open))
        {
            if ($file == '.' || $file == '..')
            {
                continue;
            }
            if (is_dir($path . "/" . $file))
            {
                $r = deleteDir($path . "/" . $file);
                if (!$r)
                    return false;
            }
            else
            {
                $r = @unlink($path . "/" . $file);
                if (!$r)
                    return false;
            }
        }
        closedir($open);
        $r = @rmdir($path);
        if (!$r)
        {
            return false;
        }
        return true;
    }

}
